package com.bishe.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bishe.entity.Role;
import com.bishe.entity.RolePermission;
import com.bishe.entity.User;
import com.bishe.entity.response.LoginInfo;
import com.bishe.entity.response.UserInfo;
import com.bishe.service.RolePermissionService;
import com.bishe.service.RoleService;
import com.bishe.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class LoginService {

    @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;

    @Resource
    private RolePermissionService rolePermissionService;

    public LoginInfo getLoginInfo(String username) {
        User user = userService.getOne(Wrappers.lambdaQuery(User.class).eq(User::getUsername,username));

        Role roles = roleService.getOne(Wrappers.lambdaQuery(Role.class).eq(Role::getUid,user.getId()));

        Set<String> permissionList = new HashSet<>();

        //此处如果多个角色都拥有某项权限，bu会数据重复，内部用的是Set
        List<RolePermission> sysPermissions = rolePermissionService.list(Wrappers.lambdaQuery(RolePermission.class).eq(RolePermission::getRoleId,roles.getId()));
        for (RolePermission perm : sysPermissions) {
            permissionList.add(perm.getPermissionCode());//权限存储
        }

        return  null;
    }

}
